/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.employee.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.employee.dao.StoreBonusSettingCareDao;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.employee.entity.StoreBonusSettingCard;
import com.chlitina.store.modules.employee.entity.StoreBonusSettingCare;
import com.chlitina.store.modules.employee.entity.StoreBonusSettingTemplateCard;
import com.chlitina.store.modules.employee.entity.StoreBonusSettingTemplateCare;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 手工护理提成设定Service
 * @author liujiang
 * @version 2013-11-15
 */
@Component
@Transactional(readOnly = true)
public class StoreBonusSettingCareService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreBonusSettingCareService.class);
	
	@Autowired
	private StoreBonusSettingCareDao storeBonusSettingCareDao;
	@Autowired
	private StoreBonusSettingTemplateCareService storeBonusSettingTemplateCareService;
	
	public StoreBonusSettingCare get(Long id) {
		return storeBonusSettingCareDao.findOne(id);
	}
	
	public Page<StoreBonusSettingCare> find(Page<StoreBonusSettingCare> page, Long employeeId, int type) {
		DetachedCriteria dc = storeBonusSettingCareDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		if (employeeId != null){
			dc.add(Restrictions.eq("employee.id", employeeId));
		}
		dc.add(Restrictions.eq("card_type", type));
		dc.addOrder(Order.desc("id"));
		return storeBonusSettingCareDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreBonusSettingCare storeBonusSettingCare) {
		storeBonusSettingCareDao.save(storeBonusSettingCare);
	}
	
	@Transactional(readOnly = false)
	public void delete(StoreBonusSettingCare storeBonusSettingCare) {
		storeBonusSettingCareDao.delete(storeBonusSettingCare);
	}
	
	public List<Long> getByCardType(Long employeeId, int type) {
		return storeBonusSettingCareDao.getByCardType(employeeId, type);
	}
	
	@Transactional(readOnly = false)
	public List<StoreBonusSettingCare> getByEmployee(Employee employee) {
		DetachedCriteria dc = storeBonusSettingCareDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		dc.add(Restrictions.eq("employee", employee));
		return storeBonusSettingCareDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void copy(Employee employee, int ifModify) {
		List<StoreBonusSettingTemplateCare> list = storeBonusSettingTemplateCareService.getByStore();
		List<StoreBonusSettingCare> existList = getByEmployee(employee);
		for (StoreBonusSettingTemplateCare stc : list) {
			StoreBonusSettingCare same = null;
			for (StoreBonusSettingCare sc : existList) {
				if(stc.getStoreCare().getId() == sc.getStoreCare().getId()
						&& stc.getCard_type() == sc.getCard_type()) {
					same = sc;
					break;
				}
			}
			if(same != null) {
				if(ifModify == 1) {
					same.setBonus_type(stc.getBonus_type());
					same.setBonus_rate(stc.getBonus_rate());
					same.setFixed_money(stc.getFixed_money());
					save(same);
				}
			} else {
				StoreBonusSettingCare storeBonusSettingCare = new StoreBonusSettingCare();
				storeBonusSettingCare.setStore(stc.getStore());
				storeBonusSettingCare.setEmployee(employee);
				storeBonusSettingCare.setStoreCare(stc.getStoreCare());
				storeBonusSettingCare.setCard_type(stc.getCard_type());
				storeBonusSettingCare.setBonus_type(stc.getBonus_type());
				storeBonusSettingCare.setBonus_rate(stc.getBonus_rate());
				storeBonusSettingCare.setFixed_money(stc.getFixed_money());
				save(storeBonusSettingCare);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void copy(Employee employee, int ifModify, Employee from) {
		List<StoreBonusSettingCare> list = getByEmployee(from);
		List<StoreBonusSettingCare> existList = getByEmployee(employee);
		for (StoreBonusSettingCare stc : list) {
			StoreBonusSettingCare same = null;
			for (StoreBonusSettingCare sc : existList) {
				if(stc.getStoreCare().getId() == sc.getStoreCare().getId()
						&& stc.getCard_type() == sc.getCard_type()) {
					same = sc;
					break;
				}
			}
			if(same != null) {
				if(ifModify == 1) {
					same.setBonus_type(stc.getBonus_type());
					same.setBonus_rate(stc.getBonus_rate());
					same.setFixed_money(stc.getFixed_money());
					save(same);
				}
			} else {
				StoreBonusSettingCare storeBonusSettingCare = new StoreBonusSettingCare();
				storeBonusSettingCare.setStore(stc.getStore());
				storeBonusSettingCare.setEmployee(employee);
				storeBonusSettingCare.setStoreCare(stc.getStoreCare());
				storeBonusSettingCare.setCard_type(stc.getCard_type());
				storeBonusSettingCare.setBonus_type(stc.getBonus_type());
				storeBonusSettingCare.setBonus_rate(stc.getBonus_rate());
				storeBonusSettingCare.setFixed_money(stc.getFixed_money());
				save(storeBonusSettingCare);
			}
		}
	}
}
